package app.netant.excepbook.qinkan;

import org.jsoup.select.Elements;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;
import app.netant.NetAntBase;

public class QinKanPage extends NetAntBase {

	public QinKanPage(String netSite) {
		super(netSite);
	}
	
	/**
	 * 信息解析
	 */
	public void analyse(){
		
		Elements activePart = document.select(".wrap .list .listBox ul li a");
		if(activePart.size()>0){
			for(int i=0;i<activePart.size();i++){	
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				String href = activePart.get(i).attr("href");
				
				if(href.startsWith("/book")){
					href = "http://www.qinkan.net" + href;
				}else{
					continue;
				}
				
				System.out.println(href + "地址开始处理.........");
				
				QinKanDetail detail = null;
				try {
					
					DataTable dataTable = this.getDBManager().executeQueryTable("select * from ${schema}.NET_QINKAN_INFO where NOVEL_URL='"+href+"'");
					if(dataTable.next()){
						continue;
					}

					QinKanEntity entity = new QinKanEntity();
					entity.setNovelUrl(href);
					
					detail = new QinKanDetail(href);		
					detail.setEntity(entity);
					
					detail.analyse();
					
				} catch (Exception e) {
					
					e.printStackTrace();
					
					DBFactory.getThreadDB().executeUpdate("insert into ${schema}.NET_QINKAN_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
														+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+href+"','2')");
					
					continue;
					
				} finally {
					if(detail != null){
						detail.free();
					}
				}
				
				Enviroment.getEnv().getLogBean().endLog();

			}
		}
			
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		QinKanPage page = new QinKanPage("http://www.qinkan.net/xhqh/");
		page.analyse();
		
		Enviroment.getEnv().getLogBean().endLog();
		
		
	}
}
